package bit.pja.wolfkill.Service;


import bit.pja.wolfkill.dao.RoomDAO;
import bit.pja.wolfkill.dao.RoomUserDAO;
import bit.pja.wolfkill.entity.AjaxResult;
import bit.pja.wolfkill.entity.Room;
import bit.pja.wolfkill.entity.RoomUser;
import bit.pja.wolfkill.entity.RoomUserKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;

//@Service
public class WolfService {

    @Autowired
    private RoomDAO roomDAO;

    @Autowired
    private RoomUserDAO roomUserDAO;

    public AjaxResult createRoom(Integer userId) {
        Integer roomId = new Random().nextInt(10000);
        Optional<Room> Oroom = roomDAO.findById(roomId);
        if (Oroom.isPresent()) {
            if (Oroom.get().getCreatedTime().getTime() - new Date().getTime() > 6*3600*1000) {
                return createRoom(userId);
            } else {
                Room room = Oroom.get();
                room.setHostID(userId);
                roomDAO.save(room);
                room = roomDAO.findById(room.getRoomId()).get();
                return AjaxResult.successData(room);
            }
        } else {
            Room room = new Room();
            room.setRoomId(roomId);
            room.setHostID(userId);
            roomDAO.save(room);
            room = roomDAO.findById(room.getRoomId()).get();
            return AjaxResult.successData(room);
        }
    }

    @Transactional
    public AjaxResult join(Integer userId, Integer roomId) throws Exception{
        Optional<Room> Oroom = roomDAO.findById(roomId);
        if (!Oroom.isPresent()) {
            throw new Exception("房间不存在");
        } else {
            Room room = Oroom.get();
            RoomUserKey key = new RoomUserKey();
            key.setRoomId(roomId);
            key.setUserId(userId);
            Optional<RoomUser> OroomUser = roomUserDAO.findById(key);
            if(OroomUser.isPresent()) {
                return AjaxResult.success();
            } else {
                if (room.getSize() >= room.getHostID()) {
                    throw new Exception("房间已满");
                } else {
                    RoomUser roomUser = new RoomUser();
                    roomUser.setRoomUserKey(key);
                    roomUserDAO.save(roomUser);
//                    room.setNowPeople(room.getNowPeople()+1);
                    roomDAO.save(room);
                    return AjaxResult.success();
                }
            }
        }
    }

    @Transactional
    public AjaxResult quit(Integer userId, Integer roomId) throws Exception{
        Optional<Room> Oroom = roomDAO.findById(roomId);
        if (!Oroom.isPresent()) {
            throw new Exception("房间不存在");
        } else {
            Room room = Oroom.get();
            RoomUserKey key = new RoomUserKey();
            key.setRoomId(roomId);
            key.setUserId(userId);
            Optional<RoomUser> OroomUser = roomUserDAO.findById(key);
            if(!OroomUser.isPresent()) {
                return AjaxResult.success();
            } else {
                roomUserDAO.delete(OroomUser.get());
//                room.setNowPeople(room.getNowPeople()-1);
                roomDAO.save(room);
                return AjaxResult.success();
            }
        }
    }

}
